<template>
  <div id="app">
    <el-config-provider :locale="zhCn">
      <!-- 导航栏 -->
      <el-header v-if="!isLoginPage" height="60px" class="header">
        <div class="nav-container">
          <div class="logo">
            <el-icon class="logo-icon"><Trophy /></el-icon>
            <span class="logo-text">编程学习平台</span>
          </div>
          <el-menu
            mode="horizontal"
            :default-active="$route.path"
            router
            class="nav-menu"
          >
            <!-- 普通用户菜单 -->
            <el-menu-item index="/dashboard" v-if="!isAdminPage">
              <el-icon><House /></el-icon>
              <span>学习概览</span>
            </el-menu-item>
            <el-menu-item index="/progress" v-if="!isAdminPage">
              <el-icon><DataLine /></el-icon>
              <span>学习进度</span>
            </el-menu-item>
            <el-menu-item index="/checkin" v-if="!isAdminPage">
              <el-icon><Calendar /></el-icon>
              <span>每日打卡</span>
            </el-menu-item>
            <el-menu-item index="/resources" v-if="!isAdminPage">
              <el-icon><Reading /></el-icon>
              <span>学习资源</span>
            </el-menu-item>
            
            <!-- 管理员菜单 -->
            <el-menu-item index="/admin/dashboard" v-if="isAdminPage">
              <el-icon><Monitor /></el-icon>
              <span>管理概览</span>
            </el-menu-item>
            <el-menu-item index="/admin/users" v-if="isAdminPage">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/resources" v-if="isAdminPage">
              <el-icon><FolderOpened /></el-icon>
              <span>资源管理</span>
            </el-menu-item>
          </el-menu>
          <div class="user-info">
            <!-- 角色切换按钮 (仅管理员可见) -->
            <el-button-group v-if="userInfo.role === 'admin'" class="role-switch">
              <el-button 
                :type="!isAdminPage ? 'primary' : 'default'" 
                size="small"
                @click="switchToUserMode"
              >
                <el-icon><User /></el-icon>
                学习模式
              </el-button>
              <el-button 
                :type="isAdminPage ? 'primary' : 'default'" 
                size="small"
                @click="switchToAdminMode"
              >
                <el-icon><Setting /></el-icon>
                管理模式
              </el-button>
            </el-button-group>
            
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                <el-avatar :size="32" :src="userInfo.avatar">
                  <el-icon><User /></el-icon>
                </el-avatar>
                <span class="username">{{ userInfo.username }}</span>
                <el-tag v-if="userInfo.role === 'admin'" type="danger" size="small" class="role-tag">管理员</el-tag>
                <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    个人信息
                  </el-dropdown-item>
                  <el-dropdown-item command="settings">
                    <el-icon><Setting /></el-icon>
                    设置
                  </el-dropdown-item>
                  <el-dropdown-item v-if="userInfo.role === 'admin'" divided command="admin">
                    <el-icon><Monitor /></el-icon>
                    管理后台
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-main :style="{ paddingTop: isLoginPage ? '0' : '20px' }">
        <router-view />
      </el-main>

      <!-- 页脚 -->
      <el-footer v-if="!isLoginPage" height="60px" class="footer">
        <div class="footer-content">
          <p>&copy; 2024 编程学习平台. 助力你的编程学习之路</p>
          <p v-if="isAdminPage" class="admin-footer">当前处于管理模式</p>
        </div>
      </el-footer>
    </el-config-provider>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

const route = useRoute()
const router = useRouter()

// 用户信息
const userInfo = ref({
  username: '学习者',
  avatar: '',
  email: '',
  role: 'student'
})

// 判断是否是登录页
const isLoginPage = computed(() => {
  return route.path === '/login' || route.path === '/register'
})

// 判断是否是管理后台页面
const isAdminPage = computed(() => {
  return route.path.startsWith('/admin')
})

// 下拉菜单命令处理
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'admin':
      switchToAdminMode()
      break
    case 'logout':
      logout()
      break
  }
}

// 切换到用户模式
const switchToUserMode = () => {
  router.push('/dashboard')
}

// 切换到管理模式
const switchToAdminMode = () => {
  if (userInfo.value.role === 'admin') {
    router.push('/admin/dashboard')
  } else {
    ElMessage.error('您没有管理员权限')
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  localStorage.removeItem('user')
  ElMessage.success('退出登录成功')
  router.push('/login')
}

// 初始化用户信息
const loadUserInfo = () => {
  const savedUserInfo = localStorage.getItem('userInfo')
  const savedUser = localStorage.getItem('user')
  
  if (savedUserInfo) {
    userInfo.value = JSON.parse(savedUserInfo)
  } else if (savedUser) {
    userInfo.value = JSON.parse(savedUser)
  }
}

// 监听路由变化，更新用户信息
watch(() => route.path, () => {
  loadUserInfo()
})

// 组件挂载时初始化
onMounted(() => {
  loadUserInfo()
})
</script>

<style scoped>
.header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.nav-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
}

.logo-icon {
  font-size: 24px;
  margin-right: 8px;
}

.nav-menu {
  flex: 1;
  margin: 0 40px;
  border-bottom: none;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.role-switch {
  margin-right: 12px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f7fa;
}

.username {
  margin: 0 8px;
  font-weight: 500;
}

.role-tag {
  margin: 0 4px;
}

.dropdown-icon {
  font-size: 12px;
  color: #909399;
}

.footer {
  background: #f8f9fa;
  border-top: 1px solid #e4e7ed;
  margin-top: 40px;
}

.footer-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #909399;
  font-size: 14px;
}

.admin-footer {
  color: #f56c6c;
  font-weight: 500;
  margin-top: 4px;
}

.el-main {
  padding-top: 80px;
  min-height: calc(100vh - 140px);
  max-width: 1200px;
  margin: 0 auto;
  padding-left: 20px;
  padding-right: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    padding: 0 10px;
  }
  
  .logo-text {
    display: none;
  }
  
  .nav-menu {
    margin: 0 20px;
  }
  
  .username {
    display: none;
  }
  
  .role-switch {
    display: none;
  }
  
  .el-main {
    padding-left: 10px;
    padding-right: 10px;
  }
}
</style> 